package com.qf.flashlib.widget;

import android.graphics.Canvas;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

/**
 * 2019/1/24 14:53
 */
public class StickyItemDecoration extends RecyclerView.ItemDecoration {

    private LinearLayoutManager mLayoutManager;

    private int mStickyViewType;

    private View mStickyItemView;

    private RecyclerView.Adapter<RecyclerView.ViewHolder> mAdapter;

    private int mStickyItemViewMarginTop;

    private RecyclerView.ViewHolder mViewHolder;

    private int mStickyItemViewHeight;


    public StickyItemDecoration(int stickyViewType) {
        this.mStickyViewType = stickyViewType;
    }


    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        mLayoutManager = (LinearLayoutManager) parent.getLayoutManager();
        int before = findBeforeStickyItemPosition(parent);
        int next = findNextStickyItemPosition(parent);
        if (before != RecyclerView.NO_POSITION) {
            getStickyViewHolder(parent);
            bindDataForStickyView(before, parent.getMeasuredWidth());
            if (next != RecyclerView.NO_POSITION) {
                View nextView = mLayoutManager.findViewByPosition(next);
                if (nextView != null && nextView.getTop() < mStickyItemViewHeight) {
                    mStickyItemViewMarginTop = mStickyItemViewHeight - nextView.getTop();
                } else {
                    mStickyItemViewMarginTop = 0;
                }
            }
            drawStickyItemView(c);
        }


    }


    /**
     * @param parent recyclerView
     * @return 第一个可见item之前的第一个stickyItem的adapter pos
     */
    private int findBeforeStickyItemPosition(RecyclerView parent) {
        int firstVisibleItemPosition = mLayoutManager.findFirstVisibleItemPosition();
        int childCount = parent.getAdapter().getItemCount();
        if (childCount < firstVisibleItemPosition)
            return RecyclerView.NO_POSITION;
        for (int i = firstVisibleItemPosition; i >= 0; i--) {
            if (mStickyViewType == parent.getAdapter().getItemViewType(i))
                return i;
        }
        return RecyclerView.NO_POSITION;
    }

    /**
     * @param parent recyclerView
     * @return 第一个可见item之后的第一个stickyItem的adapter pos
     */
    private int findNextStickyItemPosition(RecyclerView parent) {
        int firstVisibleItemPosition = mLayoutManager.findFirstVisibleItemPosition();
        int childCount = parent.getAdapter().getItemCount();
        if (childCount < firstVisibleItemPosition)
            return RecyclerView.NO_POSITION;
        for (int i = firstVisibleItemPosition + 1; i < childCount; i++) {
            if (mStickyViewType == parent.getAdapter().getItemViewType(i))
                return i;
        }
        return RecyclerView.NO_POSITION;
    }


    /**
     * 得到吸附viewHolder
     */
    private void getStickyViewHolder(RecyclerView recyclerView) {
        if (mViewHolder != null)
            return;
        mAdapter = recyclerView.getAdapter();
        mViewHolder = mAdapter.onCreateViewHolder(recyclerView, mStickyViewType);
        mStickyItemView = mViewHolder.itemView;

    }


    /**
     * 给StickyView绑定数据
     *
     * @param position data的position
     */
    private void bindDataForStickyView(int position, int parentWidth) {
        if (mAdapter == null || mViewHolder == null)
            return;
        mAdapter.onBindViewHolder(mViewHolder, position);
        measureLayoutStickyItemView(parentWidth);
        mStickyItemViewHeight = mViewHolder.itemView.getMeasuredHeight();
    }

    /**
     * 计算布局吸附的itemView
     *
     * @param parentWidth w
     */
    private void measureLayoutStickyItemView(int parentWidth) {
        if (mStickyItemView == null || !mStickyItemView.isLayoutRequested())
            return;

        int widthSpec = View.MeasureSpec.makeMeasureSpec(parentWidth, View.MeasureSpec.EXACTLY);
        int heightSpec;

        ViewGroup.LayoutParams layoutParams = mStickyItemView.getLayoutParams();
        if (layoutParams != null && layoutParams.height > 0) {
            heightSpec = View.MeasureSpec.makeMeasureSpec(layoutParams.height, View.MeasureSpec.EXACTLY);
        } else {
            heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        }

        mStickyItemView.measure(widthSpec, heightSpec);
        mStickyItemView.layout(0, 0, mStickyItemView.getMeasuredWidth(), mStickyItemView.getMeasuredHeight());
    }

    /**
     * 绘制吸附的itemView
     *
     * @param canvas c
     */
    private void drawStickyItemView(Canvas canvas) {
        if (mStickyItemView == null)
            return;

        int saveCount = canvas.save();
        canvas.translate(0, -mStickyItemViewMarginTop);
        mStickyItemView.draw(canvas);
        canvas.restoreToCount(saveCount);
    }
}
